import scrapy

class DangSpider(scrapy.Spider):
    name = 'dang'
    allowed_domains = ['movie.douban.com']
    start_urls = ['https://movie.douban.com/top250']

    def parse(self, response):
        # 提取电影信息
        movies = response.xpath('//div[@class="item"]')
        for movie in movies:
            title = movie.xpath('.//span[@class="title"]/text()').get(default='N/A')
            rating = movie.xpath('.//span[@class="rating_num"]/text()').get(default='N/A')
            link = movie.xpath('.//div[@class="hd"]/a/@href').get(default='N/A')
            image = movie.xpath('.//img/@src').get(default='N/A')
            year = movie.xpath('.//div[@class="bd"]/p[1]/text()').re_first(r'\d{4}', 'N/A')
            genre = movie.xpath('.//div[@class="bd"]/p[1]/text()').re(r'/(.+?)\s*\n')[0].strip() if movie.xpath('.//div[@class="bd"]/p[1]/text()').re(r'/(.+?)\s*\n') else 'N/A'
            director = movie.xpath('.//div[@class="bd"]/p[1]/text()').re(r'导演: (.+?)\s*\n')[0].strip() if movie.xpath('.//div[@class="bd"]/p[1]/text()').re(r'导演: (.+?)\s*\n') else 'N/A'
            actors = movie.xpath('.//div[@class="bd"]/p[2]/text()').get(default='N/A').strip()

            yield {
                'title': title,
                'rating': rating,
                'link': link,
                'image': image,
                'year': year,
                'genre': genre,
                'director': director,
                'actors': actors,
            }

        # 翻页
        next_page = response.xpath('//span[@class="next"]/a/@href').get()
        if next_page:
            yield response.follow(next_page, self.parse)
